<template>
  <a-modal
    title="编辑其他收支"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
       <a-form :form="form">
        <a-form-item v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item
          label="房間"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
            <a-input :disabled="true"  v-decorator="['roomName']">
            </a-input>
        </a-form-item>
        <a-form-item
          label="租客"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input :disabled="true" v-decorator="['name']" />
        </a-form-item>
        <a-form-item
          label="類目"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input :disabled="true" v-decorator="['revenuesTypeName']" />
        </a-form-item>
        <a-form-item
          label="金额"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
           <a-input-number
              style="width:100%"
              placeholder="請輸入金额"
              :precision="2"
              :min="0"
              :max="99999999"
              :step="10"
              v-decorator="['amount', { rules: [{required: true, message: '請輸入金额'}]}]"/>
        </a-form-item>
        <a-form-item
              label="應結款日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-date-picker style="width: 100%" placeholder="请选择應結款日期" v-decorator="['shouldReceiveDate', { rules: [{ required: true, message: '请选择應結款日期！' }]}]" @change="onChangeshouldReceiveDate"/>
            </a-form-item>
            <a-form-item
              label="收租提醒开关"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol">
              <a-radio-group placeholder="请选择收租提醒" button-style="solid" onch v-decorator="['rentReminderSwitch',{ initialValue: 0, rules: [{ required: true, message: '请选择收租提醒！' }]}]" @change="onRentReminderSwitchChange">
                          <a-radio-button  :key="0" :value="0">關閉</a-radio-button>
                          <a-radio-button  :key="1" :value="1">開啓</a-radio-button>
              </a-radio-group>
            </a-form-item>
            <div v-show="onRentReminderSwitch">
              <a-form-item
                  label="收租提醒提前 天"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol">
                  <a-select  style="width: 100%" v-decorator="['rentReminderDay', { initialValue: 1 , rules: [{ required: true, message: '请选择收租提醒提前 天！' }]}]">
                    <a-select-option  style="width: 100%"  v-for="(item,index) in 100" :key="index" :value="item">{{ item }}天</a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="收租提醒提前 小時"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol">
                  <a-select  style="width: 100%" v-decorator="['rentReminderHour', { initialValue: 0 , rules: [{ required: true, message: '请选择收租提醒提前 小時！' }]}]">
                    <a-select-option  style="width: 100%"   :key="0" :value="0">0點</a-select-option>
                    <a-select-option  style="width: 100%"  v-for="(item,index) in 23" :key="index" :value="item">{{ item }}點</a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item
                  label="收租提醒提前 分鐘"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol">
                  <a-select  style="width: 100%" v-decorator="['rentReminderMinutes', { initialValue: 0 , rules: [{ required: true, message: '请选择收租提醒提前 分鐘！' }]}]">
                    <a-select-option  style="width: 100%"  :key="0" :value="0">0分</a-select-option>
                    <a-select-option  style="width: 100%"  v-for="(item,index) in 59" :key="index" :value="item">{{ item }}分</a-select-option>
                  </a-select>
                </a-form-item>
          </div>
          <a-form-item
            label="備注"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol">
            <a-textarea placeholder="请输入備注" v-decorator="['remark']" />
          </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import { billOtherRevenuesEdit } from '@/api/modular/main/billotherrevenues/billOtherRevenuesManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        typeData: [],
        onRentReminderSwitch: false,
        shouldReceiveDateDateString: '',
        collectionTypeData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              roomName: record.roomName,
              name: record.name,
              revenuesTypeName: record.revenuesTypeName,
              amount: record.amount,
              rentReminderSwitch: record.rentReminderSwitch,
              rentReminderDay: record.rentReminderDay,
              rentReminderHour: record.rentReminderHour,
              rentReminderMinutes: record.rentReminderMinutes,
              remark: record.remark
            }
          )
        }, 100)
        if (record.rentReminderSwitch === 1) {
          this.onRentReminderSwitch = true
        }
        // 时间单独处理
        if (record.shouldReceiveDate != null) {
            this.form.getFieldDecorator('shouldReceiveDate', { initialValue: moment(record.shouldReceiveDate, 'YYYY-MM-DD') })
        }
        this.shouldReceiveDateDateString = moment(record.shouldReceiveDate).format('YYYY-MM-DD')
      },
      onRentReminderSwitchChange() {
        this.onRentReminderSwitch = !this.onRentReminderSwitch
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.shouldReceiveDate = this.shouldReceiveDateDateString
            billOtherRevenuesEdit(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('修改成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.msg)//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangeshouldReceiveDate(date, dateString) {
        this.shouldReceiveDateDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
